package com.diandi.log;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.UnsynchronizedAppenderBase;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
//import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.LinkedHashMap;

/**
 * <appender name = "mongoAppender" class="com.diandi.log.MongoDBAppender" >
 * <logMonitor>修改默认值log_monitor</logMonitor>
 * <mongoTemplateName>修改默认值mongoTemplate</mongoTemplateName>
 * </appender>
 * mongoAppender
 * logback
 * Created by 袁恩光 on 2017/11/30.
 */
@Component
public class MongoDBAppender extends UnsynchronizedAppenderBase<ILoggingEvent> implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    private String logMonitor = "log_monitor";

    private String mongoTemplateName = "mongoTemplate";

    @Override
    public void setApplicationContext(ApplicationContext ac) throws BeansException {
        applicationContext = ac;
    }

    @Override
    protected void append(ILoggingEvent eventObject) {
//        MongoTemplate mongoTemplate = (MongoTemplate) applicationContext.getBean(mongoTemplateName);
//        if (null != mongoTemplate) {
//            if (null != LogTemp.LOG_TITLE_MESSAGE.get()) {
//                LinkedHashMap<String, Object> mongo = new LinkedHashMap<>();
//                mongo.putAll(LogTemp.LOG_TITLE_MESSAGE.get());
//                mongo.put("message", eventObject.getFormattedMessage());
//                mongo.put("currentTime", new Date());
//                mongoTemplate.insert(mongo, logMonitor);
//            }
//        }
    }

    public String getLogMonitor() {
        return logMonitor;
    }

    public void setLogMonitor(String logMonitor) {
        this.logMonitor = logMonitor;
    }

    public String getMongoTemplateName() {
        return mongoTemplateName;
    }

    public void setMongoTemplateName(String mongoTemplateName) {
        this.mongoTemplateName = mongoTemplateName;
    }
}